You detect leakage by asking one question for every feature: "Could this column only be known after, or because of, the target happens?"

In your case:
- target: `cnt`
- features: `casual`, `registered`
- if `cnt = casual + registered`, then those features are not just correlated, they directly contain the answer

That is the core rule: if a feature is derived from the target, is a component of the target, or is recorded later than the prediction moment, it is leakage.

## Practical way to check for hidden leaks

### 1. Understand the business meaning of each column
A column name alone is not enough. Ask:
- What does this feature mean?
- When is it available?
- Is it known before prediction time?
- Was it computed using final outcomes?

### 2. Define the prediction timestamp
You must be precise:
- "At what exact moment am I making the prediction?"

If you want to predict bike count for an hour, only use information available before or at that hour. Anything collected after that hour is suspect.

### 3. Look for obvious target relatives
These are common leakage patterns:
- parts of the target
- post-event summaries
- IDs tied to outcome
- cumulative totals including the target period
- flags created after review/manual processing
- columns with names like `final`, `approved`, `actual`, `status`, `score`, `registered`, `total`

### 4. Check suspiciously high performance
Warning signs:
- regression `R2` extremely close to `1.0`
- classification accuracy unrealistically high
- train and test both nearly perfect on a real noisy problem

That does not prove leakage, but it is a strong signal to audit features.

### 5. Test feature importance or correlations
If one or two columns dominate completely, inspect them manually.
For example:

```python
df.corr(numeric_only=True)["cnt"].sort_values(ascending=False)
```

If some feature has correlation near `1`, ask why.

### 6. Do ablation testing
Train multiple models:
- with all features
- without suspicious features
- without date/time proxies
- without identifiers

If performance collapses after removing a column, that column may be leaking or may simply be genuinely strong. Then inspect its meaning.

### 7. Use time-based validation when data is temporal
Random split can hide leakage. For time data, always ask:
- am I accidentally training on future information?

### 8. Read the dataset documentation carefully
Many public datasets include columns that are valid for analysis but invalid for prediction. Your dataset is exactly that kind of case.

## A simple checklist you can use every time

- Is this feature available before prediction?
- Could this feature be computed from the target?
- Is this feature a later update of the same event?
- Is this feature a summary that includes future information?
- Would a real system know this value at prediction time?

If any answer is "yes" or "maybe", treat it as leakage until proven otherwise.

## A good mindset

- ML is not "use all columns except target"
- ML is "use only columns available at prediction time"

## How to judge which columns contribute more in linear regression

The line below gives a rough first clue:

```python
df.corr(numeric_only=True)["cnt"].sort_values(ascending=False)
```

What it does:
- computes correlation between every numeric column and `cnt`
- sorts the values from highest positive correlation to lowest

How to interpret it:
- correlation close to `1` means strong positive linear relationship
- correlation close to `-1` means strong negative linear relationship
- correlation close to `0` means weak linear relationship

This means columns with correlation magnitude closer to `1` or `-1` are often more useful for linear regression than columns near `0`.

For contribution, the absolute value is usually more useful than the signed value. A feature with `-0.75` can be just as useful as one with `0.75`.

Use this version:

```python
df.corr(numeric_only=True)["cnt"].drop("cnt").abs().sort_values(ascending=False)
```

This shows the strongest linear relationships with `cnt`, regardless of direction.

### Important limitation of correlation

Correlation is only a screening tool. It is not the final answer because:
- it checks one feature at a time
- it does not tell you whether a feature is leaking the target
- it does not handle overlapping information between columns
- a feature with low individual correlation can still help in combination with others

### Better next step after correlation

After removing leakage columns, train the linear regression model and inspect the coefficients.

Example:

```python
from sklearn.linear_model import LinearRegression
import pandas as pd

X = df.drop(columns=["cnt", "casual", "registered", "instant", "dteday"])
y = df["cnt"]

model = LinearRegression()
model.fit(X, y)

coef_df = pd.DataFrame({
    "feature": X.columns,
    "coefficient": model.coef_
})

print(coef_df.sort_values("coefficient", key=lambda s: s.abs(), ascending=False))
```

What this means:
- a larger coefficient magnitude usually means stronger influence on the prediction
- a positive coefficient means the prediction tends to increase as the feature increases
- a negative coefficient means the prediction tends to decrease as the feature increases

### Why scaling matters

Raw coefficients can be misleading when columns are on different scales. For proper comparison, standardize the features first.

Use:

```python
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
import pandas as pd

X = df.drop(columns=["cnt", "casual", "registered", "instant", "dteday"])
y = df["cnt"]

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

model = LinearRegression()
model.fit(X_scaled, y)

coef_df = pd.DataFrame({
    "feature": X.columns,
    "coefficient": model.coef_
})

print(coef_df.reindex(coef_df["coefficient"].abs().sort_values(ascending=False).index))
```

This gives a better idea of which features contribute more to the regression.

### Practical workflow

Use this order:
- check correlation with the target
- remove leakage or unrealistic columns
- fit linear regression
- inspect standardized coefficients
- remove weak or suspicious columns and compare model metrics such as `R2`, `RMSE`, and `MAE`

### Key idea

- correlation tells you which columns are linearly related to the target
- coefficients tell you how the fitted model is using those columns
- feature meaning tells you whether the column is valid to use at all
